package com.hu.zhcc.shiro.controller.xsf;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.xsf.dto.TEvaluateDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TEvaluate;
import com.hu.zhcc.shiro.entity.xsf.vo.TEvaluateVO;
import com.hu.zhcc.shiro.service.xsf.TEvaluateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/tEvaluate")
public class TEvaluateController {
    @Autowired
    private TEvaluateService tEvaluateService;

    @GetMapping("")
    public ResponseEntity<Page<TEvaluateVO>> getRoleList(@RequestParam(required = false) String name,
                                                        int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }
        Page<TEvaluateDTO> result = this.tEvaluateService.queryAllTEvaluate(paremeters, offset, limit);
        List<TEvaluateVO> voList = new ArrayList<TEvaluateVO>();
        if( result.getRows()!=null ) {

                for(TEvaluateDTO dto : result.getRows()) {
                TEvaluateVO vo = new TEvaluateVO();
                BeanUtils.copyProperties(dto, vo);
                voList.add(vo);
            }
    //        System.out.println(voList);
            return ResponseEntity.ok(new Page<TEvaluateVO>(result.getTotal(), voList));
        }else {
            return null;
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<TEvaluateVO> getById(@PathVariable("id") int id) {
        TEvaluateDTO dto = this.tEvaluateService.geEtById(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TEvaluateVO vo = new TEvaluateVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTEvaluate(@PathVariable("id") int id, @RequestBody TEvaluate vo) {
        int rows = this.tEvaluateService.updateTEvaluate(vo);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTEvaluate(@PathVariable("id") int id) {
        return this.tEvaluateService.removeTEvaluate(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PostMapping("")
    public ResponseEntity<TEvaluateVO> saveTEvaluate(@RequestBody TEvaluateVO vo) {
        TEvaluateDTO dto = new TEvaluateDTO();
        BeanUtils.copyProperties(vo, dto);
        dto.setCreateTime(new Date());
        this.tEvaluateService.saveTEvaluate(dto);
        vo.setId(dto.getId());
        vo.setState(0);
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }

}
